package dataManagers;

import entity.Questions;
import java.util.*;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;



public enum QuestionsDM{
    INSTANCE;

private QuestionsDM(){}
public static void createPath(Questions question){
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try{
            pm.makePersistent(new Questions(question.getPathId(),question.getLevelId(),question.getJsonFile(),question.getDownloads()));
        }catch(Exception e){

        }finally{
            pm.close();
        }

    }

    public static Questions[] retrieveAll() {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        List<Questions> result = null;
        Query query=pm.newQuery(Questions.class);
        try {

            result=(List<Questions>)query.execute();
            result.size();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            query.closeAll();
            pm.close();
        }
        return result.toArray(new Questions[result.size()]);
    }


    public static Questions retrieve(String pathId,String levelId) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Questions user = null;
        Query query = pm.newQuery(Questions.class);
        query.setFilter("pathId == pathIdParam");
        query.declareParameters("String pathIdParam");
        query.setFilter("levelId == levelIdParam");
        query.declareParameters("String pathIDParam");
        try {
           List<Questions> results = (List<Questions>) query.execute(pathId,levelId);
           if (results.iterator().hasNext()) {
                for (Questions e : results) {
                    user = e;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            query.closeAll();
            pm.close();
        }
        return user;
    }


    public static void remove(String pathId,String levelId) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query=pm.newQuery(Questions.class);
        query.setFilter("pathId == pathIdParam");
        query.declareParameters("String pathIdParam");
        query.setFilter("levelId == levelIdParam");
        query.declareParameters("String pathIDParam");

        try {
            query.deletePersistentAll(pathId,levelId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            query.closeAll();
            pm.close();
        }
    }

    public static void modify(Questions level) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        List<Questions> list=null;
        Query query=pm.newQuery(Questions.class);
        query.setFilter("pathId == pathIdParam");
        query.declareParameters("String pathIdParam");
        query.setFilter("levelId == levelIdParam");
        query.declareParameters("String pathIDParam");

        try {
            list = (List<Questions>)query.execute(level.getPathId(),level.getLevelId());
            if (list.iterator().hasNext()){
                 for(Questions e: list){
                      e.setPathId(level.getPathId());
                      e.setLevelId(level.getLevelId());
                      e.setJsonFile(level.getJsonFile());
                      e.setDownloads(level.getDownloads());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            query.closeAll();
            pm.close();
        }
    }

}
